import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_device_static_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/device_manger/device_manger_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';


const _transculentBg = Color.fromARGB(46, 253, 107, 107);

/// 设备管理
class DeviceMangerPage extends StatelessWidget {
  DeviceMangerPage({super.key});

  final controller = Get.put(DeviceMangerController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Container(
            padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: [
                _getSearchConditionWidget(),
                AppGaps.vGap14,
                Expanded(
                  child: _listType(),
                ),
              ],
            ),
          );
        });
  }

  // 1:监控类 2: 仪表类
  Widget _listType() {
    switch (controller.selectInventory?.name) {
      case '监控类':
        return _buildMonitor();
      case '仪表类':
        return _buildInstrument();
      default:
        return const Center(child: Text('请选择类别'));
    }
  }

  /// 监控类
  Widget _buildMonitor() {
    return ListView.builder(
      itemCount: controller.deviceList.length,
      itemBuilder: (context, index) {
        return Padding(
          padding: EdgeInsets.symmetric(vertical: 8.w),
          child: _buildMonitorItem(controller.deviceList[index]),
        );
      },
    );
  }

  /// 仪表类
  Widget _buildInstrument() {
    return ListView.builder(
      itemCount: controller.deviceList.length,
      itemBuilder: (context, index) {
        return Padding(
          padding: EdgeInsets.symmetric(vertical: 8.w),
          child: _buildInstrumentItem(controller.deviceList[index]),
        );
      },
    );
  }

  Widget _buildMonitorItem(WarehouseDeviceStaticEntity entity) {
    return CustomCard(
      onTap: () => Get.toNamed(
        AppRoutes.majorHazardMonitorCameraPage,
        arguments: {
          'url': entity.videoUrl,
        },
      ),
      padding: EdgeInsets.all(4.w),
      content: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(
            height: 80.w,
            width: 140.w,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  decoration: BoxDecoration(
                    color: Colors.cyan,
                    borderRadius: BorderRadius.circular(8.w),
                  ),
                ),
                SvgPicture.asset(
                  AppImages.icon_media_play,
                  height: 20.w,
                  width: 20.w,
                ),
              ],
            ),
          ),
          AppGaps.hGap10,
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                width: 120.w,
                child: Text(
                  entity.warehouseName ?? '未知',
                  style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                ),
              ),
              AppGaps.vGap6,
              Container(
                decoration: BoxDecoration(
                  color: _transculentBg,
                  borderRadius: BorderRadius.circular(4.w),
                ),
                padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 4.w),
                child: Text(entity.states ?? '未知'),
              ),
              AppGaps.vGap6,
              // if (exception != null)
              //   Row(
              //     children: [
              //       SvgPicture.asset(
              //         AppImages.icon_red_triangle,
              //         height: 8.w,
              //         width: 8.w,
              //       ),
              //       AppGaps.hGap2,
              //       Text(
              //         exception,
              //         style: TextStyle(fontSize: 12.w, color: Colors.grey),
              //       )
              //     ],
              //   ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildInstrumentItem(WarehouseDeviceStaticEntity entity) {
    return CustomCard(
      onTap: () => Get.toNamed(AppRoutes.warehouseDeviceCurve, arguments: {
        'mac': entity.mac,
        'name': entity.warehouseName,
      }),
      titleWidget: Padding(
        padding: EdgeInsets.all(14.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SvgPicture.asset(AppImages.icon_title),
                AppGaps.hGap10,
                SizedBox(
                  width: 140.w,
                  child: Text(
                    entity.warehouseName ?? '未知',
                    overflow: TextOverflow.clip,
                    style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                  ),
                ),
              ],
            ),
            Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_history_curve,
                  height: 16.w,
                  width: 16.w,
                ),
                AppGaps.hGap2,
                Text(
                  '历史曲线图',
                  style: TextStyle(fontSize: 12.w, color: AppColors.font_color_blue),
                )
              ],
            ),
          ],
        ),
      ),
      divider: CustomCard.builtinDivider,
      padding: EdgeInsets.zero,
      content: Column(
        children: [
          CustomTextWidget.getTextRowView(title: '温度', value: '${entity.temperature}'),
          AppGaps.vGap8,
          CustomTextWidget.getTextRowView(title: '湿度', value: '${entity.humidity}'),
          AppGaps.vGap8,
          CustomTextWidget.getTextRowView(title: '等级', value: entity.warehouseLevel ?? '未知'),
          AppGaps.vGap6,
          _buildItem('状态', entity.states),
        ],
      ).paddingSymmetric(horizontal: 17.w, vertical: 10.w),
    );
  }

  //获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              child: CustomDropdownSelection(
                text: controller.warehouse?.getShowName(),
                hintText: '选择所在仓库',
                onCallBack: controller.showWarehouseSelection,
              ),
            ),
            AppGaps.hGap10,
            Expanded(
              child: CustomDropdownSelection(
                text: controller.selectInventory?.name,
                hintText: '选择类别',
                onCallBack: controller.showInventorySelection,
              ),
            ),
          ],
        ),

        // _getFilter()
      ],
    );
  }

  static Widget _buildItem(String key, String? val) {
    late final Color color;
    switch (val) {
      case '正常':
        color = Colors.green;
        break;
      case '临期':
        color = Colors.yellow;
        break;
      case '到期':
        color = Colors.red;
        break;
      default:
        color = Colors.blue;
    }
    return Row(
      children: [
        Text(
          key,
          style: TextStyle(fontSize: 13.w, color: AppColors.font_color_gray),
          textAlign: TextAlign.start,
        ),
        const Spacer(),
        Container(
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(4.w),
          ),
          padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 7.w),
          child: Text(
            val ?? '未知',
            style: TextStyle(fontSize: 12.w, color: Colors.white),
            textAlign: TextAlign.end,
          ),
        ),
      ],
    );
  }
}
